其他
Spring Boot系列MyBatis配置详解
PS:你的状态取决于你的心态,要想不再焦虑,先把生活节奏规律起来。
MyBatis工作流程
依赖及配置
@Mapper和@MapperScan
实体类
Mapper配置文件
Mapper接口
Mapper映射文件
collection标签的使用
多数据源配置
测试结果
MyBatis使用注解配置
MyBatis工作流程
读取 mybatis-config.xml 配置文件;
加载 Mapper 映射文件或对应注解内容,里面定义了相应的 SQL 语句;
根据配置信息创建会话工厂
SqlSessionFactory
;根据会话工厂创建
SqlSession
,里面包含了执行 SQL 需要的所有方法;创建
Executor
执行器,用来执行 SQL 语句,在创建会话工厂SqlSessionFactory
的时候就会创建一个Executor
,其默认执行器类型是ExecutorType.SIMPLE
;MappedStatement
对象,该对象是Executor
执行器方法中的参数,主要是对 Mapper XML 文件中映射信息的封装;输入参数映射;
输出参数映射。
依赖及配置
// ...
// myBaits
// http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.1'
// mysql驱动
runtime("mysql:mysql-connector-java")
// ...
}
spring.datasource.username=root
# 数据库密码
spring.datasource.password=admin
# JDBC Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JDBC URL
spring.datasource.url=jdbc:mysql://localhost:3306/db_student?serverTimezone=Asia/Shanghai
#spring.datasource.url=jdbc:mysql://localhost:3306/db_student?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
# 是否执行MyBatis xml配置文件的状态检查, 只是检查状态,默认false
mybatis.check-config-location=true
# mybatis-config.xml文件的位置
mybatis.config-location=classpath:mybatis/mybatis-config.xml
# Mapper对应的xml路径
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
# 设置别名的路径,可避免写全限定类名
mybatis.type-aliases-package=com.manu.mybatisxml.model
@Mapper和@MapperScan
@Mapper
注解用来标记 Mapper 接口,其被标注的接口都会生成对应的动态代理类,如果有多个 Mapper 接口,则都需要用 @Mapper
注解来标注,使用方式如下:public interface ClassMapper{
///
}
@MapperScan
在项目的入口类上进行标注,可以配置要扫描的接口所在的一个或多个包,也可以使用通配符 *
来进行配置,使用方式如下:// 扫描指定包中的接口
@MapperScan("com.manu.mybatisxml.mapper")
// @MapperScan("com.manu.mybatisxml.*.mapper")
// @MapperScan({"pack1","pack2"})
public class SpringBootMybatisXmlApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisXmlApplication.class, args);
}
}
实体类
Class
如下:* 班级类
*/
public class Class {
private String classId;
private String name;
private List<Student> students;
public Class() {
}
public Class(String classId, String name) {
this.classId = classId;
this.name = name;
}
// ...
// setter、getter、toString
}
Student
类如下:* 学生类
*/
public class Student {
private String classId;
private String sno;
private String name;
private String grade;
public Student() {
}
public Student(String classId, String sno, String name, String grade) {
this.classId = classId;
this.sno = sno;
this.name = name;
this.grade = grade;
}
// ...
// setter、getter、toString
}
MyBatis配置文件
"-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<!--定义别名,避免写全限定类名-->
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
<typeAlias alias="Student" type="com.manu.mybatisxml.model.Student" />
<typeAlias alias="Class" type="com.manu.mybatisxml.model.Class" />
</typeAliases>
</configuration>
Mapper接口
id
属性子相同,ClassMapper
如下:* ClassMapper.xml对应的Mapper接口
*/
public interface ClassMapper {
/**
* 插入一条数据
* @param student student
*/
void insertStudent(Student student);
void insertClass(Class course);
/**
* 根据sno删除一条记录
* @param sno sno
*/
void deleteStudent(String sno);
/**
* 更新数据
* @param student student
*/
void updateStudent(Student student);
/**
* 更具名称查询数据
* @param name name
* @return
*/
Student findStudentByName(String name);
/**
* 查询全部数据
* @return
*/
List<Student> findAllStudent();
/**
* 集合数据查询
* @param name name
* @return
*/
Class findClassStudents(String name);
/**
* 集合数据嵌套查询
* @param classId classId
* @return
*/
Class findClassStudents1(String classId);
}
Mapper映射文件
mapper
:配置 Mapper 映射文件对应的 Mapper 接口类;resultMap
:查询语句结果集;result
:用于定义resultMap
标签中的字段;id
:用于定义resultMap
标签中的主键字段;collection
:集合数据,如List
这种数据;sql
:定义 SQL 语句块供其他 SQL 语句使用;insert
:插入语句;delete
:删除语句;update
:更新语句;select
:查询语句。
ClassMapper
对应的 Mapper 映射文件如下:"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.manu.mybatisxml.mapper.ClassMapper">
<!--Student POJO映射结果集-->
<!--id:唯一标识-->
<!--type:具体的POJO对象类型-->
<resultMap id="StudentResultMap" type="com.manu.mybatisxml.model.Student">
<!--column:主键字段也可以是查询语句中的别名字段-->
<!--property:对应POJO对象中的属性-->
<!--jdbcType:字段类型-->
<id column="classId" property="classId" jdbcType="VARCHAR" />
<!--column:表的字段-->
<result column="userName" property="name" jdbcType="VARCHAR" />
<result column="sno" property="sno" jdbcType="VARCHAR" />
<result column="grade" property="grade" jdbcType="VARCHAR" />
</resultMap>
<!--Student POJO映射结果集,携带集合结果集-->
<resultMap id="ClassWithCollectionResultMap" type="com.manu.mybatisxml.model.Class">
<id column="classId" property="classId" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<!--ofType:集合中的数据类型-->
<collection property="students" ofType="Student">
<id column="sno" property="sno" jdbcType="VARCHAR" />
<result column="userName" property="name" jdbcType="VARCHAR" />
<result column="classId" property="classId" jdbcType="VARCHAR" />
<result column="grade" property="grade" jdbcType="VARCHAR" />
</collection>
</resultMap>
<!--Student POJO映射结果集,携带集合结果集,嵌套查询-->
<resultMap id="ClassWithCollectionResultMap1" type="com.manu.mybatisxml.model.Class">
<id column="classId" property="classId" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<!--column:嵌套查询的条件-->
<!--select:嵌套查询的语句-->
<collection column="{classId = classId}" property="students" ofType="Student"
select="getStudent" />
</resultMap>
<select id="getStudent" parameterType="String" resultMap="StudentResultMap">
SELECT *
FROM mybatis_student
WHERE classId = #{classId}
</select>
<!--定义基本字段-->
<sql id="BaseStudentColumn">
sno,userName,classId,grade
</sql>
<!--插入数据-->
<!--id标识对应Mapper接口中的方法名称-->
<insert id="insertClass" parameterType="Class">
INSERT INTO mybatis_class(classId, name)
VALUES (#{classId}, #{name})
</insert>
<insert id="insertStudent" parameterType="Student">
INSERT INTO mybatis_student(classId, userName, sno, grade)
VALUES (#{classId}, #{name}, #{sno}, #{grade})
</insert>
<!--删除数据-->
<delete id="deleteStudent" parameterType="String">
DELETE
FROM mybatis_student
WHERE sno = #{sno}
</delete>
<!--更新数据-->
<update id="updateStudent" parameterType="Student">
UPDATE mybatis_student
SET userName = #{name},
classId = #{classId},
grade = #{grade},
sno = #{sno}
WHERE sno = #{sno}
</update>
<!--查询满足条件的数据集合-->
<select id="findClassStudents" parameterType="String" resultMap="ClassWithCollectionResultMap">
SELECT mybatis_class.classId,
mybatis_class.name,
mybatis_student.sno,
mybatis_student.userName,
mybatis_student.grade
FROM mybatis_student,
mybatis_class
WHERE mybatis_class.classId = mybatis_student.classId
and mybatis_class.name = #{name}
</select>
<!--查询满足条件的数据集合-->
<select id="findClassStudents1" parameterType="String"
resultMap="ClassWithCollectionResultMap1">
SELECT mybatis_class.classId,
mybatis_class.name,
mybatis_student.sno,
mybatis_student.userName,
mybatis_student.grade
FROM mybatis_student,
mybatis_class
WHERE mybatis_class.classId = mybatis_student.classId
and mybatis_class.classId = #{classId}
</select>
<!--查询单个数据-->
<select id="findStudentByName" resultMap="StudentResultMap" parameterType="String">
SELECT *
FROM mybatis_student
WHERE userName = #{name}
</select>
<!--查询全部数据-->
<select id="findAllStudent" resultMap="StudentResultMap">
SELECT
<include refid="BaseStudentColumn" />
FROM mybatis_student
</select>
</mapper>
collection标签的使用
<collection/>
标签的使用,该标签主要用来标识结果集,如班级类 Class
中的学生集合 List<Student>
,通过该标签就可以查询到指定班级的学生集合,第一种方式:<resultMap id="ClassWithCollectionResultMap" type="Class">
<id column="classId" property="classId" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<!--ofType:集合中的数据类型-->
<collection property="students" ofType="Student">
<id column="sno" property="sno" jdbcType="VARCHAR" />
<result column="userName" property="name" jdbcType="VARCHAR" />
<result column="classId" property="classId" jdbcType="VARCHAR" />
<result column="grade" property="grade" jdbcType="VARCHAR" />
</collection>
</resultMap>
<select id="findClassStudents" parameterType="String" resultMap="ClassWithCollectionResultMap">
SELECT mybatis_class.classId,
mybatis_class.name,
mybatis_student.sno,
mybatis_student.userName,
mybatis_student.grade
FROM mybatis_student,
mybatis_class
WHERE mybatis_class.classId = mybatis_student.classId
and mybatis_class.name = #{name}
</select>
<resultMap id="ClassWithCollectionResultMap1" type="com.manu.mybatisxml.model.Class">
<id column="classId" property="classId" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
<!--column:嵌套查询的条件-->
<!--select:嵌套查询的语句-->
<collection column="{classId = classId}" property="students" ofType="Student"
select="getStudent" />
</resultMap>
<select id="getStudent" parameterType="String" resultMap="StudentResultMap">
SELECT *
FROM mybatis_student
WHERE classId = #{classId}
</select>
<select id="findClassStudents1" parameterType="String"
resultMap="ClassWithCollectionResultMap1">
SELECT mybatis_class.classId,
mybatis_class.name,
mybatis_student.sno,
mybatis_student.userName,
mybatis_student.grade
FROM mybatis_student,
mybatis_class
WHERE mybatis_class.classId = mybatis_student.classId
and mybatis_class.classId = #{classId}
</select>
findClassStudents
即可查询到 Student
的对应集合。多数据源配置
SqlSessionFactory
等,主要数据源配置如下:* @Primary表示主数据源
* basePackages:指定扫描的Mapper接口
* sqlSessionTemplateRef:指定在Mapper路径下指定的SqlSessionTemplate
*/
@Configuration
@MapperScan(basePackages = "com.manu.multimybatisxml.mapper.primary",
sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class PrimaryDataSourceConfig {
@Primary
@Bean
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/primary/*.xml"));
return sessionFactoryBean.getObject();
}
@Primary
@Bean
public DataSourceTransactionManager primaryDataSourceTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean
public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Primary
,修改第二个数据源的名称以及对应的 Mapper 映射文件等,这里不再赘述。spring.datasource.primary.username=root
spring.datasource.primary.password=admin
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.jdbc-url 多数据源中用来重写自定义连接池
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/data_source_one?serverTimezone=Asia/Shanghai
# dataSourceTwo
spring.datasource.secondary.username=root
spring.datasource.secondary.password=admin
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/data_source_two?serverTimezone=Asia/Shanghai
# 是否执行MyBatis xml配置文件的状态检查, 只是检查状态,默认false
mybatis.check-config-location=true
# mybatis-config.xml文件的位置
mybatis.config-location=classpath:mybatis/mybatis-config.xml
# 设置别名的路径,可避免写全限定类名
mybatis.type-aliases-package=com.manu.multimybatisxml.model
测试结果
* MyBatisTest
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyBatisTest {
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
private ClassMapper mClassMapper;
@Test
public void insert() {
Class class1 = new Class("class1", "一班");
Class class2 = new Class("class2", "二班");
mClassMapper.insertClass(class1);
mClassMapper.insertClass(class2);
List<Student> students = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Student student;
if (i % 2 == 0) {
student = new Student("class1", "sno" + i, "Student"+i, "A");
} else {
student = new Student("class2", "sno" + i, "Student"+i, "B");
}
mClassMapper.insertStudent(student);
}
}
@Test
public void deleteStudentBySno() {
mClassMapper.deleteStudent("sno0");
}
@Test
public void updateStudent() {
Student student = new Student("class1","sno1","student1","C");
mClassMapper.updateStudent(student);
}
@Test
public void findStudentByName() {
Student student = mClassMapper.findStudentByName("student5");
System.out.println(student);
}
@Test
public void findAllStudent() {
List<Student> students = mClassMapper.findAllStudent();
for (Student student : students) {
System.out.println(student.toString());
}
}
@Test
public void findClassStudents(){
Class clazz = mClassMapper.findClassStudents("一班");
System.out.println("classId:"+clazz.getClassId()+",name:"+clazz.getName());
List<Student> students = clazz.getStudents();
for (Student student : students) {
System.out.println(student.toString());
}
}
@Test
public void findClassStudents1(){
Class clazz = mClassMapper.findClassStudents1("class1");
System.out.println("classId:"+clazz.getClassId()+",name:"+clazz.getName());
List<Student> students = clazz.getStudents();
for (Student student : students) {
System.out.println(student.toString());
}
}
}
findClassStudents
方法为例查看执行结果如下:Student{classId='class1', sno='sno1', name='student1', grade='C'}
Student{classId='class1', sno='sno2', name='Student2', grade='A'}
Student{classId='class1', sno='sno4', name='Student4', grade='A'}
Student{classId='class1', sno='sno6', name='Student6', grade='A'}
Student{classId='class1', sno='sno8', name='Student8', grade='A'}
注解配置
public interface StudentMapper {
/**
* 注解中的SQL语句中会自动获取对象student的相关属性
*/
@Insert("INSERT INTO mybatis_student(userName,sno,grade) VALUES(#{name},#{sno},#{grade})")
void insert(Student student);
/**
* StudentFactory中会自动获取对象student的相关属性在SQL语句中
* StudentFactory中insert2方法通过#{属性名}的方式获取变量值
*/
@InsertProvider(type = StudentFactory.class, method = "insert1")
void insert1(Student student);
/**
* 直接传递参数
* StudentFactory中insert2方法通过#{变量名}的方式获取变量值
* 此外也可以通过StringBuffer拼接SQL,如在insert2方法中拼接SQL字符串返回
*/
@InsertProvider(type = StudentFactory.class, method = "insert2")
void insert2(String sno, String name, String grade);
}
public String insert1(Student student) {
String sql = new SQL() {{
INSERT_INTO("mybatis_student");
VALUES("sno", "#{sno}");
VALUES("userName", "#{name}");
VALUES("grade", "#{grade}");
}}.toString();
System.out.println("SQL:" + sql);
return sql;
}
public String insert2(String sno,String name,String grade) {
String sql = new SQL() {{
INSERT_INTO("mybatis_student");
VALUES("sno", "#{sno}");
VALUES("userName", "#{name}");
VALUES("grade", "#{grade}");
}}.toString();
System.out.println("SQL:" + sql);
return sql;
}
}
@SpringBootTest
public class MyBatisAnnotationTests {
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired
StudentMapper mStudentMapper;
@Test
public void insert() {
Student student = new Student("sno0", "jzman0", "A");
mStudentMapper.insert(student);
}
@Test
public void insert1() {
Student student = new Student("sno1", "jzman1", "A");
mStudentMapper.insert1(student);
}
@Test
public void insert2() {
Student student = new Student("sno2", "jzman2", "A");
mStudentMapper.insert2(student.getSno(), student.getName(), student.getGrade());
}
}